<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名" v-model="roleName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="getRoleList"
          >查询</el-button
        >
        <el-button @click="reset">清空</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="showSaveRole">添加</el-button>
    <el-button type="danger">批量删除</el-button>

    <el-table :data="roleList" border class="role-table">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="用户名" prop="roleName" />
      <el-table-column label="操作" width="200" fixed="right">
        <template v-slot="{ row }">
          <el-tooltip content="分配角色权限" placement="top" effect="light">
            <el-button
              type="primary"
              :icon="UserFilled"
              size="small"
              @click="showRoleAuth(row.id, row.roleName)"
            />
          </el-tooltip>
          <el-tooltip content="修改角色" placement="top" effect="light">
            <el-button type="warning" :icon="Edit" size="small" />
          </el-tooltip>
          <el-tooltip content="删除角色" placement="top" effect="light">
            <el-button type="danger" :icon="Delete" size="small" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, jumper, total, sizes, "
      :total="total"
      small
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </el-card>

  <el-dialog v-model="saveRoleVisible" title="添加角色">
    <el-form
      label-width="100px"
      :model="roleFormData"
      :rules="rules"
      ref="roleFormRef"
    >
      <el-form-item label="角色名" prop="roleName">
        <el-input
          placeholder="角色名"
          v-model="roleFormData.roleName"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="saveRole">确定</el-button>
        <el-button @click="saveRoleVisible = false">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
export default {
  name: "myRole",
};
</script>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import { Edit, Delete, UserFilled, Search } from "@element-plus/icons-vue";
import { getRoleListApi, saveRoleApi } from "@/api/acl/role";
import type { RoleList } from "@/api/acl/model/roleModel";
import { ElMessage, type FormInstance } from "element-plus";
import { useRouter } from "vue-router";

const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(1);

const roleName = ref("");
const roleList = ref<RoleList>([]);

const router = useRouter();

// 显示给角色分配权限的页面
const showRoleAuth = (id: string, roleName: string) => {
  router.push({
    name: "RoleAuth",
    query: {
      id,
      roleName,
    },
  });
};

// 获取角色列表
const getRoleList = async () => {
  const result = await getRoleListApi({
    roleName: roleName.value,
    page: currentPage.value,
    limit: pageSize.value,
  });
  roleList.value = result.items;
  total.value = result.total;
};
// 清空按钮
const reset = () => {
  roleName.value = "";
  getRoleList();
};

onMounted(getRoleList);

const handleCurrentChange = (newCurrentPage: number) => {
  currentPage.value = newCurrentPage;
  getRoleList();
};
const handleSizeChange = (newPageSize: number) => {
  pageSize.value = newPageSize;
  getRoleList();
};

const saveRoleVisible = ref(false);
const roleFormRef = ref<FormInstance>();
const roleFormData = reactive({
  roleName: "",
});
const rules = {
  roleName: [{ required: true, message: "请输入角色名", trigger: "blur" }],
};
// 显示添加角色
const showSaveRole = () => {
  roleFormRef.value?.clearValidate();
  roleFormData.roleName = "";
  saveRoleVisible.value = true;
};
// 添加角色
const saveRole = async () => {
  await roleFormRef.value?.validate();
  await saveRoleApi(roleFormData.roleName);
  ElMessage.success("添加角色成功");
  saveRoleVisible.value = false;
  getRoleList();
};
</script>

<style scoped>
.user-table {
  margin: 10px 0;
}
</style>
